<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/11
 * Time: 11:22
 */

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;

class LoanModel
{
    const TABLE = 'loan';

    /**
     * @param array $data
     * @return mixed
     */
    public function getAccountsList(array $data)
    {
        return DB::table(self::TABLE)
            ->where(function($query)use ($data){
                if (!empty($data['employeeId'])){
                    $query->where('employeeId',$data['employeeId']);
                }
                if (!empty($data['startTime'])){
                    $query->where('loanTime','>=',$data['startTime']);
                }
                if (!empty($data['endTime'])){
                    $query->where('loanTime','<=',$data['endTime']);
                }
            })
            ->get()->toArray();
    }

    /**
     * @param array $data
     * @return array
     */
    public function info(array $data)
    {
        $result = DB::table(self::TABLE)->where($data)->first();
        return empty($result) ? [] : get_object_vars($result);
    }

    /**
     * @param array $param
     * @return mixed
     */
    public function lists(array $param)
    {
        $limit = config('yucheng.limit');
        $start = 0;
        if (isset($param['length']) && !is_null($param['length'])) {
            $limit = $param['length'];
        }
        if (isset($param['page']) && !is_null($param['page'])) {
            $start = ($param['page']-1)*$limit;
        }
        return DB::table(self::TABLE)
            ->leftJoin('employee as e','e.id','=',self::TABLE.'.employeeId')
            ->leftJoin('project as p','p.id','=',self::TABLE.'.projectId')
            ->leftJoin('admin as a','a.id','=',self::TABLE.'.adminId')
            ->where(function($query)use($param){
                if (!empty($param['projectId'])){
                    $query->where(self::TABLE.'.projectId',$param['projectId']);
                }
                if (!empty($param['bookId'])){
                    $query->where(self::TABLE.'.bookId',$param['bookId']);
                }
                if (!empty($param['professionId'])){
                    $query->where('e.professionId',$param['professionId']);
                }
                if (!empty($param['employeeId'])){
                    $query->where(self::TABLE.'.employeeId',$param['employeeId']);
                }
                if (isset($param['search']) && $param['search'] != ''){
                    $query->where('e.name','like','%'.$param['search'].'%');
                }
                if (!empty($param['month'])){
                    $startTime = date($param['month'].'-01 00:00:00');
                    $endTime = date($param['month'].'-t 23:59:59');
                    $query->whereBetween('loanTime', [$startTime,$endTime]);
                }else{
                    if (isset($param['startTime']) && !is_null($param['startTime'])) {
                        $query->where('loanTime', '>=', $param['startTime'] . " 00:00:00");
                    }
                    if (isset($param['endTime']) && !is_null($param['endTime'])) {
                        $query->where('loanTime', '<=', $param['endTime'] . " 23:59:59");
                    }
                }
            })
            ->orderBy('loanTime','desc')
            ->offset($start)->limit($limit)
            ->select(self::TABLE.'.*','p.name as projectName','e.name as employeeName','a.name as adminName')
            ->get()->toArray();
    }

    /**
     * @param array $param
     * @return mixed
     */
    public function countLists(array $param){
        return DB::table(self::TABLE)
            ->leftJoin('employee as e','e.id','=',self::TABLE.'.employeeId')
            ->leftJoin('project as p','p.id','=',self::TABLE.'.projectId')
            ->where(function($query)use($param){
                if (!empty($param['projectId'])){
                    $query->where(self::TABLE.'.projectId',$param['projectId']);
                }
                if (!empty($param['bookId'])){
                    $query->where(self::TABLE.'.bookId',$param['bookId']);
                }
                if (!empty($param['professionId'])){
                    $query->where('e.professionId',$param['professionId']);
                }
                if (isset($param['search']) && $param['search'] != ''){
                    $query->where('e.name','like','%'.$param['search'].'%');
                }
                if (!empty($param['month'])){
                    $startTime = date($param['month'].'-01 00:00:00');
                    $endTime = date($param['month'].'-t 23:59:59');
                    $query->whereBetween('loanTime', [$startTime,$endTime]);
                }else{
                    if (isset($param['startTime']) && !is_null($param['startTime'])) {
                        $query->where('loanTime', '>=', $param['startTime'] . " 00:00:00");
                    }
                    if (isset($param['endTime']) && !is_null($param['endTime'])) {
                        $query->where('loanTime', '<=', $param['endTime'] . " 23:59:59");
                    }
                }
            })
            ->count();
    }

    public function insert(array $data)
    {
        $data['createTime'] = date('Y-m-d H:i:s');
        return DB::table(self::TABLE)->insert($data);
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function getSum(array $data)
    {
        return DB::table(self::TABLE)
            ->leftJoin('employee as e','e.id','=',self::TABLE.'.employeeId')
            ->where(function($query)use($data){
                if (!empty($data['projectId'])){
                    $query->where(self::TABLE.'.projectId',$data['projectId']);
                }
                if (!empty($data['bookId'])){
                    $query->where(self::TABLE.'.bookId',$data['bookId']);
                }
                if (!empty($data['professionId'])){
                    $query->where('e.professionId',$data['professionId']);
                }
                if (!empty($data['startTime'])){
                    $query->where('loanTime','>=',$data['startTime']);
                }
                if (!empty($data['endTime'])){
                    $query->where('loanTime','<=',$data['endTime']);
                }
                if (!empty($data['employeeId'])){
                    $query->where('employeeId',$data['employeeId']);
                }
                if (isset($data['status'])){
                    if (!is_array($data['status'])){
                        $query->where(self::TABLE.'.status',$data['status']);
                    }else{
                        $query->whereIn(self::TABLE.'.status',$data['status']);
                    }
                }
            })->sum('account');
    }

    public function update(array $where, array $data)
    {
        return DB::table(self::TABLE)
            ->where(function($query)use($where){
                if (!empty($where['id'])){
                    $query->where('id',$where['id']);
                }
                if (!empty($where['projectId'])){
                    $query->where('projectId',$where['projectId']);
                }
                if (!empty($where['bookId'])){
                    $query->where('bookId',$where['bookId']);
                }
                if (!empty($where['employeeId'])){
                    $query->where('employeeId',$where['employeeId']);
                }
                if (!empty($where['status'])){
                    $query->where('status',$where['status']);
                }
                if (!empty($where['startTime']) && !empty($where['endTime'])){
                    $query->whereBetween('loanTime',[$where['startTime'],$where['endTime']]);
                }
            })
            ->update($data);
    }

    public function delete(array $data)
    {
        return DB::table(self::TABLE)->where($data)->delete();
    }
}